<?php
/*
 * @Author: zwx
 * @Date: 2024-05-08 10:39:41
 * @LastEditors: zwx
 * @LastEditTime: 2024-06-11 21:21:15
 * @Description: 私域直播严禁词
 */

namespace app\apiadmin\controller\live;

use app\apiadmin\service\TCSS\TImService;
use app\common\controller\BaseController;
use app\common\utils\Res;
use app\Request;
use think\facade\Db;

class CommentGroup extends BaseController
{

    /**
     * @author: zwx
     * @Description: 获取严禁词组列表
     * @Date: 2024-05-08 15:41:00
     */
    public function getCommentGroup()
    {
        $param = $this->request->param();

        $size = $param['size'] ? $param['size'] : 10; //每页显示条数
        $page = $param['page'] ? $param['page'] : 1; //当前页数

        $input = $param['input'] ? $param['input'] : false; //输入的内容

        $where = array();
        $where[] = ['del', '=', 0];
        if ($input) {
            $where[] = array('name', 'like', '%' . $input . '%');
        }

        $data = array();
        $result = DB::table('xh_siyu_pinglun_group')->where($where)->order('id desc')->page($page, $size)->select();
        $count = DB::table('xh_siyu_pinglun_group')->where($where)->count();
        $data = [
            'list' => $result,
            'total' => $count,
        ];

        return Res::ok($data);
    }

    /**
     * @author: zwx
     * @Description: 创建严禁词组
     * @Date: 2024-05-09 11:21:47
     */
    public function createCommentGroup()
    {
        $param = $this->request->param();
        $name = $param['name']; //名称

        if (!$name || $name == '') {
            return Res::error('名称不能为空');
        }

        $data = array();
        $data['name'] = $name;
        $data['remark'] = $param['remark'];
        $data['createtime'] = date('Y-m-d H:i:s');
        $id = DB::table('xh_siyu_pinglun_group')->insert($data);

        return Res::ok($id);

    }

    /**
     * @author: zwx
     * @Description: 删除严禁词组
     * @Date: 2024-05-09 14:16:52
     */
    public function delCommentGroup()
    {

        $param = $this->request->param();
        $id = $param['id'];

        $data = DB::table('xh_siyu_pinglun_group')->where(array('id' => $id))->find();
        if ($data) {
            $a = DB::table('xh_siyu_pinglun_group')->where(array('id' => $id))->update(array('del' => 1));
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

    /**
     * @author: zwx
     * @Description: 修改严禁词组
     * @Date: 2024-05-09 11:21:47
     */
    public function editCommentGroup()
    {

        $param = $this->request->param();
        $id = $param['id'];
        $name = $param['name']; //名称
        $remark = $param['remark']; //备注

        $data = array();
        if ($name && $name != '') {
            $data['name'] = $name;
        }
        if ($remark && $remark != '') {
            $data['remark'] = $remark;
        }

        $dd = DB::table('xh_siyu_pinglun_group')->where(array('id' => $id))->find();

        if ($dd) {
            $a = DB::table('xh_siyu_pinglun_group')->where(array('id' => $id))->save($data);
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

        return Res::ok($id);

    }

    /**
     * @author: zwx
     * @Description: 批量删除严禁词组
     * @Date: 2024-05-09 14:16:52
     */
    public function pldelCommentGroup()
    {

        $param = $this->request->param();
        $ids = $param['ids'];

        $where = array();
        $where[] = ['id', 'in', $ids];

        $data = DB::table('xh_siyu_pinglun_group')->where($where)->select();
        if ($data) {
            $a = DB::table('xh_siyu_pinglun_group')->where($where)->update(array('del' => 1));
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

    /************************     评论组详情                *****************************888 */

    /**
     * @author: zwx
     * @Description: 获取评论组详情列表
     * @Date: 2024-05-08 15:41:00
     */
    public function getdata()
    {
        $param = $this->request->param();

        $size = $param['size'] ? $param['size'] : 10; //每页显示条数
        $page = $param['page'] ? $param['page'] : 1; //当前页数

        $input = $param['input'] ? $param['input'] : false; //输入的内容

        $groupid = $param['groupid'] ? $param['groupid'] : 0; //评论组ID

        $where = array();
        $where[] = ['del', '=', 0];
        if ($input) {
            $where[] = array('nick', 'like', '%' . $input . '%');
        }

        if ($groupid > 0) {
            $where[] = ['groupid', '=', $groupid];
        }

        $result = DB::table('xh_siyu_pinglun_list')->where($where)->order('id desc')->page($page, $size)->select();
        $count = DB::table('xh_siyu_pinglun_list')->where($where)->count();
        $data = [
            'list' => $result,
            'total' => $count,
        ];

        return Res::ok($data);
    }

    /**
     * @author: zwx
     * @Description: 创建评论组详情
     * @Date: 2024-05-09 11:21:47
     */
    public function createPingluninfo()
    {

        $param = $this->request->param();
        $nick = $param['nick']; //名称
        $groupid = $param['groupid']; //评论组id
        $pinglun = $param['pinglun']; //评论内容
        $fasong_time = $param['fasong_time']; //发送时间
        $touxiang_img = $param['touxiang_img']; //头像
        if (!$nick || $nick == '') {
            return Res::error('昵称不能为空');
        }
        if (!$groupid || $groupid == '') {
            return Res::error('评论组id不可为空');
        }
        if (!$pinglun || $pinglun == '') {
            return Res::error('评论内容不可为空');
        }
        if (!$fasong_time || $fasong_time == '') {
            return Res::error('发送时间不可为空');
        }
        if (!$touxiang_img || $touxiang_img == '') {
            return Res::error('头像不可为空');
        }

        $data = array();
        $data['nick'] = $nick;
        $data['groupid'] = $groupid;
        $data['pinglun'] = $pinglun;
        $data['fasong_time'] = $fasong_time;
        $data['touxiang_img'] = $touxiang_img;

        $data['createtime'] = date('Y-m-d H:i:s');

        $imService = new TImService();
        $insertId = null;
        Db::startTrans();
        try {

            $insertShuziren = array();
            $insertShuziren['nick'] = $nick;
            $insertShuziren['pinglun'] = $pinglun;
            $insertShuziren['flagid'] = 9;
            $insertShuziren['touxiang_img'] = $touxiang_img;
            $insertShuziren['createtime'] = date('Y-m-d H:i:s');

            $Pid = $this->addOrGetPid($nick, $insertShuziren);
            $insertId = $Pid;
            $data["pid"] = $Pid;
            // $id = DB::table('xh_siyu_pinglun_list')->insert($data);
            // $insertId = "P_" . $nick;
            // // 添加账号
            // $ret = $imService->account_import_one($insertId, $data["nick"], $data["touxiang_img"]);
            // if ($ret !== true) {
            //     throw new \Exception("导入账号失败");
            // }
            $id = DB::table('xh_siyu_pinglun_list')->insert($data);

            Db::commit();
            return Res::ok($id);

        } catch (\Throwable $th) {

            if (!empty($insertId)) {
                $imService->account_delete_ids([$insertId]);
            }
            Db::rollback();
            // throw $th;
            return Res::error([
                "msg" => $th->getMessage(),
                "Exception" => $th,
            ]);
        }
    }

    //  查询或者设置 用户的pid
    public function addOrGetPid($nickName, $insertShuziren)
    {
        $imService = new TImService();

        $Pid = DB::table('xh_siyu_shuziren_list')->where(array('nick' => $nickName))->value('id');
        if (empty($Pid)) {
            $insertId = DB::table('xh_siyu_shuziren_list')->insert($insertShuziren, true);
            $insertIdForIM = "R_" . env("Live.live_pre") . $insertId;
            // 添加账号
            $imService->account_import_one($insertIdForIM, $insertShuziren["nick"], $insertShuziren["touxiang_img"]);
            return $insertId;
        } else {
            return $Pid;
        }
    }

    /**
     * @author: zwx
     * @Description: 删除评论详情
     * @Date: 2024-05-09 14:16:52
     */
    public function delPingluninfo()
    {
        $param = $this->request->param();
        $id = $param['id'];

        $data = DB::table('xh_siyu_pinglun_list')->where(array('id' => $id))->find();
        if ($data) {
            $a = DB::table('xh_siyu_pinglun_list')->where(array('id' => $id))->update(array('del' => 1));
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

    /**
     * @author: zwx
     * @Description: 修改严禁词
     * @Date: 2024-05-09 11:21:47
     */
    public function editPingluninfo()
    {

        $param = $this->request->param();
        $id = $param['id'];
        $nick = $param['nick']; //名称
        $pinglun = $param['pinglun']; //评论内容
        $fasong_time = $param['fasong_time']; //发送时间
        $touxiang_img = $param['touxiang_img']; //头像
        $data = array();

        if ($nick && $nick != '') {
            $data['nick'] = $nick;
        }
        if ($pinglun && $pinglun != '') {
            $data['pinglun'] = $pinglun;
        }
        if ($fasong_time && $fasong_time != '') {
            $data['fasong_time'] = $fasong_time;
        }
        if ($touxiang_img && $touxiang_img != '') {
            $data['touxiang_img'] = $touxiang_img;
        }

        $dd = DB::table('xh_siyu_pinglun_list')->where(array('id' => $id))->find();

        if ($dd) {
            $a = DB::table('xh_siyu_pinglun_list')->where(array('id' => $id))->save($data);
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }
        return Res::ok($id);

    }

    /**
     * @author: zwx
     * @Description: 批量删除严禁词
     * @Date: 2024-05-09 14:16:52
     */
    public function pldelPingluninfo()
    {

        $param = $this->request->param();
        $ids = $param['ids'];

        $where = array();
        $where[] = ['id', 'in', $ids];

        $data = DB::table('xh_siyu_pinglun_list')->where($where)->select();
        if ($data) {
            $a = DB::table('xh_siyu_pinglun_list')->where($where)->update(array('del' => 1));
            return Res::ok($a);
        } else {
            return Res::error('未查询到数据，请刷新重试');
        }

    }

}
